package com.gitee.huxing.course.conc.conc03.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc03.stream
 * @ClassName: StreamParallelDemo
 * @Author: huxingmale
 * @DateTime: 2021-08-25 23:41
 */
public class StreamParallelDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        IntStream.range(1, 10000).forEach(i->list.add(i));
        BlockingDeque<Long> blockingDeque = new LinkedBlockingDeque<>(10000);
        // 并行处理方式
        List<Long> longlist = list.stream().parallel()
                .map(i->i.longValue()).sorted().collect(Collectors.toList());
        // 串行处理方式
//        List<Long> longlist = list.stream()
//                .map(i->i.longValue()).sorted().collect(Collectors.toList());
        // 线程处理
        longlist.stream().forEach(i->{
            try {
                blockingDeque.put(i);
            } catch (InterruptedException ex){
                ex.printStackTrace();
            }
        });
        System.out.println("blockingDeque: " + blockingDeque.toString());
    }
}
